#include "MeshObject.h"
#include "ResourceManager.h"
#include <math.h>
#include <iostream>
#include <nds.h>

MeshObject::MeshObject() :  m_meshName(""),
							m_texName(""),
							m_currentAnim("stand"),
							m_position(Vector3(0,0,0)),
							m_velocity(Vector3(0,0,0)),
							m_rotation(0),
							m_animTimer(0),
							m_bMoving(false)
{
}

bool MeshObject::Load(File* pFile)
{
	char mesh[50], tex[50], anim[50];
	float x, y, z;

	pFile->GetString(mesh);
	m_meshName.assign(mesh);
	std::cout << "Mesh File = " << m_meshName << "\n";

	pFile->GetString(tex);
	m_texName.assign(tex);
	std::cout << "tex File = " << m_texName << "\n";

	pFile->GetString(anim);
	m_currentAnim.assign(anim);
	std::cout << "anim = " << m_currentAnim << "\n";

	m_position = Vector3(pFile->GetFloat(&x), pFile->GetFloat(&y), pFile->GetFloat(&z));
	std::cout << "position = " << x << " " << y << " " << z << "\n";

	return true;
}

void MeshObject::Update(float gameSpeed)
{
	m_position += m_velocity * gameSpeed;

	if(m_animTimer < 1.0f)
	{
		m_animTimer += gameSpeed;
	}
	else
	{
		m_animTimer = 0;
	}

	Animate();
}

void MeshObject::Draw()
{
	TheResourceManager::Instance()->GetMesh(m_meshName)->SetAnimation(m_currentAnim.c_str());

	glPushMatrix();

	//glTranslatef(m_position.x, m_position.y, m_position.z);

	glRotatef(-90, 1.0f, 0.0f, 0.0f);
	glRotatef(90, 0.0f, 0.0f, 1.0f);
	//glRotatef(m_rotation - 90, 0.0f, 0.0f, 1.0f);

	TheResourceManager::Instance()->GetMesh(m_meshName)->Draw(m_animTimer, m_texName);

	glPopMatrix(1);
}

void MeshObject::Animate()
{
	if(m_bMoving)
	{
		m_currentAnim = "run";
	}
	else
	{
		m_currentAnim = "stand";
	}
}